package com.gan.yidademo.common.util;

import cn.hutool.json.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponseBody;
import com.aliyun.dingtalkyida_1_0.models.GetOpenUrlHeaders;
import com.aliyun.dingtalkyida_1_0.models.GetOpenUrlRequest;
import com.aliyun.dingtalkyida_1_0.models.GetOpenUrlResponse;
import com.aliyun.dingtalkyida_1_0.models.GetOpenUrlResponseBody;
import com.aliyun.tea.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dingtalkyida_1_0.Client;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.tools.reflect.Sample;
import org.springframework.stereotype.Service;

/**
 * @author gbw
 * @date 2023/9/22
 */
@Service
@Slf4j
public class YiDaUtil {


    /**
     * 初始化Client
     *
     * @return Client
     * @throws Exception
     */
    public static Client createClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new Client(config);
    }

    /**
     * 初始化获取 token 的客户端
     *
     * @return
     * @throws Exception
     */
    public static com.aliyun.dingtalkoauth2_1_0.Client createTokenClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }


    /**
     * 根据应用的ak、sk获取token信息
     *
     * @param ak
     * @param sk
     * @return
     */
    public static String getTokenFromApp(String ak, String sk) throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client client = YiDaUtil.createTokenClient();
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
                .setAppKey(ak)
                .setAppSecret(sk);
        GetAccessTokenResponseBody tokenResp = new GetAccessTokenResponseBody();
        try {
            tokenResp = client.getAccessToken(getAccessTokenRequest).getBody();
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                log.error("宜搭获取token报错 ====== code={}，message={}", err.code, err.message);
            }
        }
        return tokenResp.accessToken;
    }

    /**
     * 获取图片的临时免登地址
     *
     * @return
     */
    public static String getTemporaryExemptionURL(String token, String userId, String fileUrl, String systemToken, String appType) {
        Client client = null;
        try {
            client = YiDaUtil.createClient();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        GetOpenUrlHeaders getOpenUrlHeaders = new GetOpenUrlHeaders();
        getOpenUrlHeaders.xAcsDingtalkAccessToken = token;
        GetOpenUrlRequest getOpenUrlRequest = new GetOpenUrlRequest()
                .setSystemToken(systemToken)
                .setUserId(userId)
                .setFileUrl(fileUrl)
                .setTimeout(60000L);
        try {
            GetOpenUrlResponse openUrlWithOptions = client.getOpenUrlWithOptions(appType, getOpenUrlRequest, getOpenUrlHeaders, new RuntimeOptions());
            GetOpenUrlResponseBody body = openUrlWithOptions.getBody();
            String url = body.getResult();
            return url;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                throw new RuntimeException(err);
            }
        }
        return "";
    }
}
